<template>
  <div class="form-item">
    <label></label>
    <div>
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "vFormItem",
  inject: ["form"],
  props: {
    label: {
      type: String,
      default: "",
    },
    prop: {
      type: String,
    },
  },
  // item组件被渲染时，应将组件的实例对象派发给父组件
  mounted() {
    // 如果没有传入prop，则无需校验，也就无需缓存
    if (this.prop) {
      // 向vForm派发on-form-item-add事件，同时传递this
      this.dispatch("vForm", "on-form-item-add", this);
      this.setRules();
    }
  },
  methods: {
    setRules() {
      this.$on("on-form-change", this.onFieldChange);
      this.$on("on-form-blur", this.onFieldBlur);
    },
  },
  // 在组件销毁前将实例从父组件缓存中移除
  beforeDestroy() {
    this.dispatch("vForm", "on-form-item-remove", this);
  },
};
</script>

<style>
</style>